/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.eightbears.beboy.ui.chat.Adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.cache.GroupCache;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.greendao.GroupList;

import java.util.ArrayList;
import java.util.List;


public class GroupAdapter extends ArrayAdapter<GroupList> {

    private LayoutInflater inflater;
    private List<GroupList> searchList;

    public GroupAdapter(Context context, int res, List<GroupList> groups) {
        super(context, res, groups);
        this.inflater = LayoutInflater.from(context);
        String newGroup = context.getResources().getString(R.string.The_new_group_chat);
        String addPublicGroup = context.getResources().getString(R.string.add_public_group_chat);
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (getItemViewType(position) == 0) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.em_search_bar_with_padding, parent, false);
            }
            final EditText query = (EditText) convertView.findViewById(com.eightbears.beboy.R.id.query);
            final ImageButton clearSearch = (ImageButton) convertView.findViewById(R.id.search_clear);
            query.addTextChangedListener(new TextWatcher() {
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    clear();
                    if (s.toString().length() == 0) {
                        searchList = GroupCache.getInstance(getContext()).getAllData();
                        if (searchList != null) {
                            addAll(searchList);
                        }

                    } else {
                        searchList = GroupCache.getInstance(getContext()).getGroupListByName(s.toString());
                        if (searchList == null) {
                            addAll(new ArrayList<GroupList>());
                        } else {
                            addAll(searchList);
                        }

                    }
                    notifyDataSetChanged();
                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                public void afterTextChanged(Editable s) {

                }
            });
            clearSearch.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    query.getText().clear();
                }
            });
        } else {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.em_row_group, parent, false);
            }
            ((TextView) convertView.findViewById(R.id.name)).setText(getItem(position - 1).getGroupname());
            CommonUtils.loadImageView(getContext(), getItem(position - 1).getCover(),
                    R.mipmap.qun_touxiang, (ImageView) convertView.findViewById(R.id.avatar));
        }
        return convertView;
    }


    @Override
    public int getCount() {
        return super.getCount() + 1;
    }

}
